﻿using System.Security.Cryptography;
using LM.InterfacesFabricas.DTOs;
using LM.InterfacesFabricas.Fabricas;
using LM.InterfacesFabricas.Interfaces.Servicos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LM.Tela.Desktop
{
    public partial class frmLogin : Form
    {

        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogar_Click(object sender, EventArgs e)
        {
            Logar();
        }
        private void Logar()
        {
            string senha;
            using (var md5 = MD5.Create())
            {
               senha = GetMd5Hash(md5, txtSenha.Text);
            }
            var operador = new DtoOperador { Nome = txtUsuario.Text, Senha = senha };

            var servicoDeOperador = FabricaGenerica.Crie<IServicoDeAutenticar>();

            if (servicoDeOperador.Autentica(operador))
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("Dados inválidos.", "Alerta", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }

        }

        /// <summary>
        /// Metodo que gera hashing MD5 de uma string.
        /// </summary>
        /// <param name="md5Hash">Instancia da classe MD5.</param>
        /// <param name="input">String a ser encriptada.</param>
        /// <returns>String encriptada.</returns>
        public static string GetMd5Hash(MD5 md5Hash, string input)
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            frmOperador Operador = new frmOperador();
            Operador.ShowDialog();
        }

        private void btnLimpar_Click(object sender, EventArgs e)
        {
            LimpeCamposEdicao();
        }
        private void LimpeCamposEdicao()
        {
            txtUsuario.Text = string.Empty;
            txtSenha.Text = string.Empty;

        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            
        }

        private void frmLogin_Shown(object sender, EventArgs e)
        {
            txtUsuario.Focus();
        }

        private void btnSair_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void txtSenha_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Logar();
            }
        }

        
    }
}
